﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace peonwar
{
        [Serializable]
    class UnitHeavy2 : Unit
    {
        // ---CONSTRUCTORS---
        // Creation de l'unite en precisant l'instance du joueur "User"
        public UnitHeavy2(User player)
            : base()
        {
            // Caracteristique au type d'unité
            UnitType = 8;
            QueueOffset = 70;

            Initialize(player);
            AnimationFactor = 0.40;
        }

        // ---PROPERTIES---
        // ---METHODES---
        // Animation idle qui joue en boucle
        public override void AnimationIdle()
        {
            base.AnimationIdle();
            Texture = GameTexture.TextureUnitHeavy2[0];
            Imgwidth = 75;
            Imgheight = 56;
            Indexmax = 1;

            Offset = 0;
        }

        // Animation walk qui est jouer en boucle une fois lancer
        public override void AnimationWalk()
        {
            base.AnimationWalk();
            Texture = GameTexture.TextureUnitHeavy2[1];
            Imgwidth = 75;
            Imgheight = 57;
            Indexmax = 5;

            Offset = 0;
        }

        // Animation degain de l'arme qui lance ensuite l'animation tirer
        public override void AnimationDrawIn()
        {
            base.AnimationDrawIn();
            Texture = GameTexture.TextureUnitHeavy2[2];
            Imgwidth = 75;
            Imgheight = 61;
            Indexmax = 6;

            Offset = 0;
        }

        // Animation tirer joue en boucle
        public override void AnimationFire()
        {
            base.AnimationFire();
            Texture = GameTexture.TextureUnitHeavy2[3];
            Imgwidth = 75;
            Imgheight = 61;
            Indexmax = 11;

            Offset = 0;
        }

        // Animation rengainer l'arme qui lance ensuite l'animation marcher
        public override void AnimationDrawOut()
        {
            base.AnimationDrawOut();
            Texture = GameTexture.TextureUnitHeavy2[4];
            Imgwidth = 75;
            Imgheight = 61;
            Indexmax = 6;

            Offset = 0;
        }

        // Animation mourrir, pas besoin de l'expliquer
        public override void AnimationDie()
        {
            base.AnimationDie();
            Texture = GameTexture.TextureUnitHeavy2[5];
            Imgwidth = 100;
            Imgheight = 68;
            Indexmax = 5;

            Offset = 0;
        }

        protected override void Fire(bool firebase, User enemy)
        {
            int sign = -1;
            if (TeamBlue)
                sign = 1;

            Vector2 P = FindEnemy(enemy.PeonList, 0);
            if (P == Vector2.Zero)
                P = new Vector2((enemy.SpawnPosition.X) - (sign * 200), this.Position.Y);

            User.EffectList.Add(new AnimatedEffect(P.X, P.Y - 20, TeamBlue, GameTexture.TextureEffects[1], 62, 63, 3));
            Random rand = new Random();

            float d = 0f;
            for (int i = (int)this.Position.X + (25 * sign); i * sign < (int)P.X * sign; i += 4 * sign)
            {
                d++;
                double vect = rand.NextDouble() * (Math.PI / 8);
                Vector2 dir = new Vector2((float)Math.Cos(vect) * sign, (float)Math.Sin(vect));
                int r = rand.Next(3);
                User.AdvertList.Add(new Advert(new Vector2(i, P.Y - 30 - rand.Next(5) + (d / 1.5f)), r == 0 ? GameData.Font_Cursor_Little : GameData.Font_Cursor_Medium, r == 0 ? "'" : r == 1 ? "'" : "~", 0.1f + rand.Next(2), dir, 0.5f, r == 0 ? Color.Orange : r == 1 ? Color.Gray : Color.DarkGray, true, true));
            }
        }

        private Vector2 FindEnemy(List<Unit> list, int index)
        {
            if (index < list.Count())
            {
                if (list[index].DeadTime == 0)
                    return list[index].Position;
                else
                    return FindEnemy(list, index + 1);
            }
            else
                return new Vector2(0, 0);
        }

        public override void PlaySound(int x, float vol)
        {
            float f = 0;
            if (SettingManager.CurrentProfil.Pan)
            {
                float midwidth = (Game.camera.ViewWidth / 2);
                f = this.Position.X - (Game.camera.Position + midwidth);
                f = Tools.Born(-midwidth, midwidth, f);
                f = f / midwidth;
            }
            if (x == 2)
            {
                GameSound.Explosions[random.Next(3)].Play(vol * 0.6f, 0, f);
            }
            else
                GameSound.UnitSound[UnitType - 1, x].Play(vol, 0, f);
        }

        public override void Die(User player, User playerenemy, int hisIndex, int ticks)
        {
            if (CurrentAnim != "DIE")
                User.EffectList.Add(new AnimatedEffect(Position.X, Position.Y - 55, TeamBlue, GameTexture.TextureEffects[2], 90, 111, 20));
            base.Die(player, playerenemy, hisIndex, ticks);

        }
    }
}

